using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using System.Collections.Generic;

namespace IQIGame.Onigao.GamePlay
{
    public class UIFriend_Shield : UIBaseNode
    {
        #region Template Generate,don't modify
        protected class UIB_UIFriend_Shield
        {
            #region ObjectBinding Generate
            public UnityEngine.UI.ExLoopVerticalScrollRect loopVSRFriendShield { protected set; get; }
            public UnityEngine.GameObject empty { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<UnityEngine.UI.ExLoopVerticalScrollRect>("loopVSRFriendShield", out var __tbv0);
                this.loopVSRFriendShield = __tbv0;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("empty", out var __tbv1);
                this.empty = __tbv1;
            }
            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify



        #region fields
        private LoopScrollAdapter<UI_FriendShieldMould> friendCells = null;

        public List<FriendData> friendShieldList { get; set; } = new(32);

        #endregion fields

        #region properties

        protected UIB_UIFriend_Shield ui { get; set; }
        protected UIMsgDispatcher uiMsgDispatcher => this._msgDispatcher as UIMsgDispatcher;

        #endregion properties

        protected override void BeforeInit()
        {
            ui = new UIB_UIFriend_Shield();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            this.friendCells = this.InitLoopScroll<UI_FriendShieldMould>(this.ui.loopVSRFriendShield, this.OnSheildChanged);
        }


        protected override void OnShow()
        {
            this.ui.empty.SetActive(false);

            UpdateFriendCells();
        }

        protected override void OnHide()
        {

        }



        /// <summary>
        /// 添加UGUI侦听
        /// </summary>
        protected override void AddUGUIListener()
        {

        }

        /// <summary>
        /// 添加全局侦听（MsgDispatcher）
        /// </summary>
        protected override void AddMsgListeners()
        {
            uiMsgDispatcher.AddListener(MsgEventType.FriendChanged, OnFriendChanged);
        }

        protected override void OnDispose()
        {
            this.friendShieldList = null;
        }



        #region methonds

        public void UpdateFriendCells()
        {
            this.InitDataList();
            this.friendCells.RefillCellsAndSelected(friendShieldList.Count, 0);
            if (friendShieldList.Count == 0) this.ui.empty.SetActive(true);
        }

        public void InitDataList()
        {
            this.friendShieldList.Clear();
            var dic = FriendModule.Instance.GetShieldListData();

            foreach (var item in dic)
            {
                friendShieldList.Add(item.Value);
            }
        }
        private void OnSheildChanged(UI_FriendShieldMould module, int arg2)
        {
            var svData = this.friendShieldList[arg2];
            module.Show(svData);
        }
        private void OnFriendChanged()
        {
            if (isActive) UpdateFriendCells();
        }
        #endregion methonds
    }
}